package io.github.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.config.aop.service.BaseAopService;
import io.github.dao.SysRoleDao;
import io.github.entity.SysRoleEntity;
import io.github.service.SysRoleMenuService;
import io.github.service.SysRoleService;
import io.github.service.SysUserRoleService;
import io.github.util.MethodNameUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 角色
 *
 * @author Created by 思伟 on 2020/6/6
 */
@Service
public class SysRoleServiceImpl extends BaseAopService<SysRoleServiceImpl, SysRoleDao, SysRoleEntity>
        implements SysRoleService {

    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Override
    public List<SysRoleEntity> queryList(Map<String, Object> map) {
        return baseMapper.queryList(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SysRoleEntity role) {
        baseMapper.insert(role);
        // 保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role) {
        baseMapper.updateNoMapper(role);
        // 更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        baseMapper.deleteBatch(roleIds);
    }

    @Override
    public Page<SysRoleEntity> queryListByPage(Integer offset, Integer limit, String roleName, String sort,
                                               Boolean isAsc) {
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper(new SysRoleEntity());
        if (StringUtils.isNoneBlank(sort) && null != isAsc) {
            wrapper.orderBy(true, isAsc, MethodNameUtil.camel2underStr(sort));
        }
        if (StringUtils.isNoneBlank(roleName)) {
            wrapper.like("role_name", roleName);
        }
        Page<SysRoleEntity> page = new Page<SysRoleEntity>(offset, limit);
        return this.page(page, wrapper);
    }

}
